<?php

class UserController extends Zend_Controller_Action {

    public function init() {
        /* Initialize action controller here */
        $userdata = new Zend_Session_Namespace('userdata');
        if (isset($userdata->userdata)) {
            //mina
            //if is alerady login redirct user to index page
            //  $this->_helper->redirector('index', 'index');
             if($this->_request->isXmlHttpRequest())
            {                     
                    $this->_helper->viewRenderer->setNoRender();
                    $this->_helper->layout()->disableLayout();
            }
        }
    }
    public function profileAction() {
        $login = new Zend_Session_Namespace('login');
        $table = new Application_Model_User();
        $result= $login->userObj;
        $this->view->result = $result;
        $form = new Application_Form_Profile();
        $this->view->form = $form;
        if ($this->getRequest()->isPost()) {
            if ($form->isValid($_POST)) {
                $data = $form->getValues();
                if ($data['password'] != $data['confirmPassword']) { // password correct
                    $this->view->errorMessage = "Password and confirm password don't match.";
                    return;
                }
                $id=$result[0]['id'];//$this->getRequest()->getParam("id");
                if ($table->checkUserValid($data['username'],$id)) { // username is unique?
                    $this->view->errorMessage = "Name already taken. Please choose another one.";
                    return;
                }
                unset($data['confirmPassword']);
                $data['token']=$result[0]['token'];
                $data['password'] = SHA1($data['password'].$data['token']);
                $where = $table->getAdapter()->quoteInto('id = ?',$id );
                $table->update($data, $where);
                $login->username = $data['username'];
                $db = Zend_Db_Table_Abstract::getDefaultAdapter();
                $uid = $db->select()->from('users')->where("username=?", $login->username);
                $s = $db->fetchAll($uid);
                $login->userObj=$s;
                $this->_redirect('user/profile');
            }
        }
    }

  }
  
?>
